package com.jm.datagather.alarm.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jm.datagather.alarm.service.TestAlarmSentinelService;
import com.jm.datagather.user.api.example.FeignExampleUserApi;
import com.jm.datagather.user.api.example.SeataExampleUserApi;
import com.jm.datagather.user.api.example.SentinelExampleUserApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/alarm/example")
public class TestAlarmFeinController {

    @Autowired
    private FeignExampleUserApi feignExampleUserApi;
    @Autowired
    private SentinelExampleUserApi sentinelExampleUserApi;
    @Autowired
    private SeataExampleUserApi seataExampleUserApi;

    @Autowired
    private TestAlarmSentinelService testAlarmSentinelService;


    @GetMapping("/testFeign")
    public String testFeign(){

        return  feignExampleUserApi.testFeign();
    }

    @GetMapping("/testSentinel")
    public String testSentinel(){

        return  sentinelExampleUserApi.testSentinel();
    }

    @GetMapping("/testSeata")
    public String testSeata(){

        return  seataExampleUserApi.testSeata();
    }

    @GetMapping("/testSentinel1")
    public String testSentinel1(){
        log.info("时间：{},testSentinel1",new Date());
        return  "sentinel 啊，规则啊，流控啊";
    }


    @GetMapping("/testFlowLimit1")
    public String testFlowLimit1(){
        //调用其他服务流控失效，需要怎么配置？
        //return  sentinelExampleUserApi.testFlowLimit();
        return  testAlarmSentinelService.testFlowLimit();
    }

    @GetMapping("/testFlowLimit2")
    public String testFlowLimit2(){
        //调用其他服务流控失效，需要怎么配置？
       // return  sentinelExampleUserApi.testFlowLimit();
        return  testAlarmSentinelService.testFlowLimit();
    }

    @GetMapping("/testFusing")
    public String testFusing(){
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("熔断时间：{},testFusing",new Date());
        return  "sentinel 啊，规则啊，熔断";
    }


    @GetMapping("/byResource")
    @SentinelResource(value = "byResourceSentinel",blockHandler = "byResourceHandler")
    public String byResource(){
        return  "sentinel byResource，规则啊，流控啊";
    }

    public String byResourceHandler(BlockException blockException){

        return "触发 限流 byResource";
    }

    @GetMapping("/byResourceForFallback/{p}")
    @SentinelResource(value = "byResourceForFallback",blockHandler = "byResourceFallbackHandler",fallback = "byResourceFallback")
    public String byResourceForFallback(@PathVariable("p") Integer p){
        if(p == 1){
            throw new RuntimeException("p=1");
        }
        return  "sentinel byResourceForFallback，规则啊，流控啊";
    }

    public String byResourceFallbackHandler(@PathVariable("p") Integer p,BlockException blockException){

        return "触发 限流 byResourceFallbackHandler";
    }

    public String byResourceFallback(@PathVariable("p") Integer p,Throwable e){

        return "异常 e: " + e.getMessage();
    }

    /**
     * 授权 http://127.0.0.1:8201/alarm/example/testSentinelEmpower?serverName=test
     * sentinel授权增加黑名单test
     * @return
     */
    @GetMapping("/testSentinelEmpower")
    public String testSentinelEmpower(){
        log.info("时间：{},testSentinelEmpower",new Date());
        return  "testSentinelEmpower 啊，规则啊，授权啊";
    }


}
